package leetcode.code1248;

public class Solution {
	public int numberOfSubarrays(int[] nums, int k) {
		int l = 0, r = 0, ans = 0, sum = 0;
		// [2,2,2,1,2,2,1,2,2,2]
		while (r < nums.length) {
			while (r < nums.length && sum < k) {
				int num = nums[r];
				if ((num & 1) == 1) {
					sum += 1;
				}
				r++;
			}
			int R = 1;
			while (r < nums.length && (nums[r] & 1) != 1) {
				r++;
				R++;
			}
			int L = 0;
			while (sum == k) {
				L++;
				int num = nums[l];
				if ((num & 1) == 1) {
					sum -= 1;
				}
				l++;
			}
			ans += L * R;
		}
		return ans;
	}
}
